<template>
  <div class="four">
    <el-menu
      class="el-menu-demo"
      mode="horizontal"
      active-text-color="#0da5fa"
      default-active="1"
    >
      <el-menu-item index="1" @click="changeNumber(1)">前端学科</el-menu-item>
      <el-menu-item index="2" @click="changeNumber(2)">JAVA学科</el-menu-item>
    </el-menu>
    <div id="main4" style="width: 100%; height: 100%" />
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  data() {
    return {
      option: {
        xAxis: {
          type: 'category',
          data: [
            '一月',
            '二月',
            '三月',
            '四月',
            '五月',
            '六月',
            '七月',
            '八月',
            '九月',
            '十月',
            '十一月',
            '十二月'
          ],
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              color: '#55afdc'
            }
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: true,
            lineStyle: {
              color: '#55afdc'
            }
          }
        },
        grid: {
          top: '10%', // grid组件距上下左右的距离
          left: '0%',
          right: '4%',
          bottom: '0%',
          containLabel: true // grid区域是否包含坐标轴的刻度标签。
        },
        series: [
          {
            data: [100, 120, 161, 134, 105, 160, 165, 100, 120, 161, 134, 105],
            type: 'line',
            smooth: true
          }
        ],
        tooltip: {
          trigger: 'axis',
          show: true,
          // 坐标轴指示器
          axisPointer: {
            type: 'cross', // 十字准星指示器,表示启用两个正交的轴的 axisPointer
            label: {
              backgroundColor: 'rgba(0,122,255,0.6)', // 文本标签的背景颜色
              // 文本标签文字的格式化器
              formatter(a) {
                // 这里可以控制台输出一下参数a，看看里面有什么，再进行格式化
                // axisDimension可以判断是X轴还是Y轴，分别格式化X轴和Y轴的文字
                if (a.axisDimension === 'y') {
                  return parseFloat(a.value).toFixed(1)
                } else {
                  return a.value
                }
              }
            }
          }
        }
      }
    }
  },
  watch: {
    option: {
      handler(val) {
        this.option = val
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    var chartDom = document.getElementById('main4')
    var myChart = echarts.init(chartDom)
    this.option && myChart.setOption(this.option)
  },
  methods: {
    changeNumber(num) {
      if (num === 1) {
        this.option.series = [
          {
            data: [100, 120, 161, 134, 105, 160, 165, 100, 120, 161, 134, 105],
            type: 'line',
            smooth: true
          }
        ]
      } else if (num === 2) {
        this.option.series = [
          {
            data: [200, 192, 120, 144, 160, 130, 140, 145, 160, 165, 100, 120],
            type: 'line',
            smooth: true
          }
        ]
      }
      var chartDom = document.getElementById('main4')
      var myChart = echarts.init(chartDom)
      this.option && myChart.setOption(this.option)
    }
  }
}
</script>

<style lang="scss" scoped>
.four {
  width: 600px;
  height: 400px;
}
</style>
